<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title> @vue/cli 3.0 使用 svg-sprite-loader 加载本地 SVG 文件 - 早起的年轻人</title>
    <meta name="renderer" content="webkit" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>

<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />

<meta name="theme-color" content="#f8f5ec" />
<meta name="msapplication-navbutton-color" content="#f8f5ec">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="#f8f5ec">


<meta name="author" content="早起的年轻人" /><meta name="description" content="Vue 脚手架升级 3.0，项目也随之升级。Vue cli 3.0 使用 svg-symbol(svg 雪碧图)，整个 iconfont 的图标库为你所用，加快开发速度。Ant-Design 也使用这种方式引入图标。
" /><meta name="keywords" content="管理, Github, Vue, flutter, Go" />






<meta name="generator" content="Hugo 0.79.1 with even 4.0.0" />


<link rel="canonical" href="https://luckly.work/post/2019/2019-04-03-vue-cli-svg-symbol/" />
<link href="/post/2019/2019-04-03-vue-cli-svg-symbol/" rel="alternate" type="application/rss+xml" title="早起的年轻人" />
<link href="/post/2019/2019-04-03-vue-cli-svg-symbol/" rel="feed" type="application/rss+xml" title="早起的年轻人" />
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="manifest" href="/manifest.json">
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">

<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<link href="/dist/even.06658218.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.css" integrity="sha256-7TyXnr2YU040zfSP+rEcz29ggW4j56/ujTPwjMzyqFY=" crossorigin="anonymous">
<link rel="stylesheet" href="/css/reset-even.css">


<meta property="og:title" content=" @vue/cli 3.0 使用 svg-sprite-loader 加载本地 SVG 文件" />
<meta property="og:description" content="Vue 脚手架升级 3.0，项目也随之升级。Vue cli 3.0 使用 svg-symbol(svg 雪碧图)，整个 iconfont 的图标库为你所用，加快开发速度。Ant-Design 也使用这种方式引入图标。" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://luckly.work/post/2019/2019-04-03-vue-cli-svg-symbol/" />
<meta property="article:published_time" content="2019-04-03T16:34:59+08:00" />
<meta property="article:modified_time" content="2019-04-03T16:34:59+08:00" />
<meta itemprop="name" content=" @vue/cli 3.0 使用 svg-sprite-loader 加载本地 SVG 文件">
<meta itemprop="description" content="Vue 脚手架升级 3.0，项目也随之升级。Vue cli 3.0 使用 svg-symbol(svg 雪碧图)，整个 iconfont 的图标库为你所用，加快开发速度。Ant-Design 也使用这种方式引入图标。">
<meta itemprop="datePublished" content="2019-04-03T16:34:59+08:00" />
<meta itemprop="dateModified" content="2019-04-03T16:34:59+08:00" />
<meta itemprop="wordCount" content="314">



<meta itemprop="keywords" content="Vue,Svg-Symbol," />
<meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content=" @vue/cli 3.0 使用 svg-sprite-loader 加载本地 SVG 文件"/>
<meta name="twitter:description" content="Vue 脚手架升级 3.0，项目也随之升级。Vue cli 3.0 使用 svg-symbol(svg 雪碧图)，整个 iconfont 的图标库为你所用，加快开发速度。Ant-Design 也使用这种方式引入图标。"/>

</head>
<body>
<div id="mobile-navbar" class="mobile-navbar">
    <div class="mobile-header-logo">
        <a href="/" class="logo">早起的年轻人</a>
    </div>
    <div class="mobile-navbar-icon">
        <span></span>
        <span></span>
        <span></span>
    </div>
</div>
<nav id="mobile-menu" class="mobile-menu slideout-menu">
    <ul class="mobile-menu-list">
        <a href="/">
            <li class="mobile-menu-item" title="首页">首页</li>
        </a>
        <a href="/post/">
            <li class="mobile-menu-item" title="归档">归档</li>
        </a>
        <a href="/categories/">
            <li class="mobile-menu-item" title="分类">分类</li>
        </a>
        <a href="/tags/">
            <li class="mobile-menu-item" title="标签">标签</li>
        </a>
        <a href="/about/">
            <li class="mobile-menu-item" title="关于我">关于我</li>
        </a>
        <a href="/friend/">
            <li class="mobile-menu-item" title="友链">友链</li>
        </a>
        <a href="https://github.com/ITmxs/">
            <li class="mobile-menu-item" title="阅读清单">阅读清单</li>
        </a>
        
    </ul>
    <form class="search-form" style="position: absolute;">
    <input name="search" type="search" style="height: 28px;padding-left: 30px;"/>
    <input type="image" src="/img/search.svg" alt="Search" style="position: absolute;left: 6px;top: 6px;"/>
</form>


</nav>

<div class="container" id="mobile-panel">
    <header id="header" class="header">
        <div class="logo-wrapper">
  <a href="/%20/" class="logo">早起的年轻人</a>
</div>

<nav class="site-navbar">
  <ul id="menu" class="menu">
    <li class="menu-item">
      <a class="menu-item-link" href="/">首页</a>
    </li><li class="menu-item">
      <a class="menu-item-link" href="/post/">归档</a>
    </li><li class="menu-item">
      <a class="menu-item-link" href="/categories/">分类</a>
    </li><li class="menu-item">
      <a class="menu-item-link" href="/tags/">标签</a>
    </li><li class="menu-item">
      <a class="menu-item-link" href="/about/">关于我</a>
    </li><li class="menu-item">
      <a class="menu-item-link" href="/friend/">友链</a>
    </li><li class="menu-item">
      <a class="menu-item-link" href="https://github.com/ITmxs/">阅读清单</a>
    </li>
  </ul>
</nav>
        <form class="search-form" style="position: absolute;">
    <input name="search" type="search" style="height: 28px;padding-left: 30px;"/>
    <input type="image" src="/img/search.svg" alt="Search" style="position: absolute;left: 6px;top: 6px;"/>
</form>



    </header>


    <main id="main" class="main">
        <div class="content-wrapper">
            <div id="content" class="content">
                <article class="post">
  
  <header class="post-header">
    <h1 class="post-title"> @vue/cli 3.0 使用 svg-sprite-loader 加载本地 SVG 文件</h1>

    <div class="post-meta">
      <span class="post-time"> 2019-04-03 16:34 </span>
      <div class="post-category">
        <a href="/categories/%E6%8A%80%E6%9C%AF/"> 技术 </a>
        </div>
      <span class="more-meta"> 约 314 字 </span>
      <span class="more-meta"> 预计阅读 1 分钟 </span>
      <span id="busuanzi_container_page_pv" class="more-meta"> <span id="busuanzi_value_page_pv"><img src="/img/spinner.svg" alt="spinner.svg"/></span> 次阅读 </span>
    </div>
  </header>

  <div class="post-toc" id="post-toc">
  <h2 class="post-toc-title">文章目录</h2>
  <div class="post-toc-content">
    <nav id="TableOfContents">
  <ul>
    <li>
      <ul>
        <li><a href="#运行">运行</a></li>
        <li><a href="#使用">使用</a></li>
        <li><a href="#配置-svg-sprite-loader">配置 <strong>svg-sprite-loader</strong></a></li>
        <li><a href="#调用当前环境下的颜色">调用当前环境下的颜色</a></li>
        <li><a href="#props">props</a></li>
        <li><a href="#代码">代码</a></li>
      </ul>
    </li>
  </ul>
</nav>
  </div>
</div>
  <div class="post-outdated">
    <div class="warn">
      <p>【注意】最后更新于 <span class="timeago" datetime="2019-04-03T16:34:59" title="April 3, 2019">April 3, 2019</span>，文中内容可能已过时，请谨慎使用。</p>
    </div>
  </div>
  <div class="post-content">
    <p>Vue 脚手架升级 3.0，项目也随之升级。<a href="https://cli.vuejs.org/">Vue cli 3.0</a> 使用 svg-symbol(svg 雪碧图)，整个 <a href="https://www.iconfont.cn">iconfont</a> 的图标库为你所用，加快开发速度。Ant-Design 也使用这种方式引入图标。</p>
<h2 id="运行">运行</h2>
<ol>
<li>装包 <code>yarn</code> or <code>npm i</code></li>
<li>运行 <code>npm run serve</code></li>
</ol>
<h2 id="使用">使用</h2>
<p>使用 <strong>svg-symbol</strong> 引入图标</p>
<p>svg 文件会导入到 HTML 文件中，无需使用两张 png 切换</p>
<p><code>./src/main.js</code> 引入 <code>import &quot;./icons&quot;;</code></p>
<p>在 <code>./src/icons/svg</code> 文件夹下的 svg 文件会自动导入项目</p>
<ol>
<li><a href="https://www.iconfont.cn">https://www.iconfont.cn</a> 下载图标</li>
<li>下载的 svg 图标放在 <code>./src/icons/svg</code> 文件夹下</li>
<li>为了使用当前环境下的颜色，去掉 svg 的 fill 属性</li>
<li>调用 <code>&lt;svg-icon icon-name=&quot;home&quot; class-name=&quot;test&quot;&gt;&lt;/svg-icon&gt;</code></li>
</ol>
<h2 id="配置-svg-sprite-loader">配置 <strong>svg-sprite-loader</strong></h2>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-js" data-lang="js"><span class="c1">// ./vue.config.js
</span><span class="c1"></span><span class="nx">module</span><span class="p">.</span><span class="nx">exports</span> <span class="o">=</span> <span class="p">{</span>
  <span class="nx">chainWebpack</span><span class="o">:</span> <span class="nx">config</span> <span class="p">=&gt;</span> <span class="p">{</span>
    <span class="kr">const</span> <span class="nx">svgRule</span> <span class="o">=</span> <span class="nx">config</span><span class="p">.</span><span class="nx">module</span><span class="p">.</span><span class="nx">rule</span><span class="p">(</span><span class="s2">&#34;svg&#34;</span><span class="p">);</span>
    <span class="nx">svgRule</span><span class="p">.</span><span class="nx">uses</span><span class="p">.</span><span class="nx">clear</span><span class="p">();</span>
    <span class="nx">svgRule</span>
      <span class="p">.</span><span class="nx">use</span><span class="p">(</span><span class="s2">&#34;svg-sprite-loader&#34;</span><span class="p">)</span>
      <span class="p">.</span><span class="nx">loader</span><span class="p">(</span><span class="s2">&#34;svg-sprite-loader&#34;</span><span class="p">)</span>
      <span class="p">.</span><span class="nx">options</span><span class="p">({</span>
        <span class="nx">symbolId</span><span class="o">:</span> <span class="s2">&#34;icon-[name]&#34;</span><span class="p">,</span>
        <span class="nx">include</span><span class="o">:</span> <span class="p">[</span><span class="s2">&#34;./src/icons&#34;</span><span class="p">]</span>
      <span class="p">});</span>
  <span class="p">}</span>
<span class="p">};</span>
</code></pre></td></tr></table>
</div>
</div><h2 id="调用当前环境下的颜色">调用当前环境下的颜色</h2>
<blockquote>
<p>当前环境的颜色改变，图标颜色也会跟着改变</p>
</blockquote>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span><span class="lnt">8
</span><span class="lnt">9
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-html" data-lang="html"><span class="c">&lt;!--SvgIcon--&gt;</span>
<span class="p">&lt;</span><span class="nt">style</span><span class="p">&gt;</span>
  <span class="p">.</span><span class="nc">svg-icon</span> <span class="p">{</span>
    <span class="k">width</span><span class="p">:</span> <span class="mi">1</span><span class="kt">em</span><span class="p">;</span>
    <span class="k">height</span><span class="p">:</span> <span class="mi">1</span><span class="kt">em</span><span class="p">;</span>
    <span class="n">fill</span><span class="p">:</span> <span class="kc">currentColor</span><span class="p">;</span> <span class="c">/*关键*/</span>
    <span class="k">overflow</span><span class="p">:</span> <span class="kc">hidden</span><span class="p">;</span>
  <span class="p">}</span>
<span class="p">&lt;/</span><span class="nt">style</span><span class="p">&gt;</span>
</code></pre></td></tr></table>
</div>
</div><h2 id="props">props</h2>
<table>
<thead>
<tr>
<th>props</th>
<th>说明</th>
<th>type</th>
</tr>
</thead>
<tbody>
<tr>
<td>iconName</td>
<td>svg 文件名，不要加上后缀</td>
<td>string</td>
</tr>
<tr>
<td>className</td>
<td>图标样式类</td>
<td>string</td>
</tr>
</tbody>
</table>
<h2 id="代码">代码</h2>
<p><a href="https://nusr.github.io/vue-svg/dist">online demo</a>
<a href="https://github.com/nusr/vuecli3.0-svg">代码地址</a></p>
  </div>

  <div class="post-copyright">
  <p class="copyright-item">
    <span class="item-title">文章作者</span>
    <span class="item-content">早起的年轻人</span>
  </p>
  <p class="copyright-item">
    <span class="item-title">上次更新</span>
    <span class="item-content">
        2019-04-03 16:34
        
    </span>
  </p>
  
  <p class="copyright-item">
    <span class="item-title">许可协议</span>
    <span class="item-content"><a target="_blank" rel="license noopener" href="https://github.com/nusr/blog/blob/master/LICENSE">MIT</a></span>
  </p>
</div>
<div class="post-reward">
  <input type="checkbox" name="reward" id="reward" hidden />
  <label class="reward-button" for="reward">赞赏支持</label>
  <div class="qr-code">
    
    <label class="qr-code-image" for="reward">
        <img class="image" src="/images/wechat.jpg">
        <span>微信佛系打赏</span>
      </label>
    <label class="qr-code-image" for="reward">
        <img class="image" src="/images/alipay.jpg">
        <span>支付宝佛系打赏</span>
      </label>
  </div>
</div><footer class="post-footer">
    <div class="post-tags">
      <a href="/tags/Vue/">Vue</a>
      <a href="/tags/Svg-Symbol/">Svg-Symbol</a>
      </div>
    <nav class="post-nav">
      <a class="prev" href="/post/2019/2019-04-13-mac-sublime/">
        <i class="iconfont icon-left"></i>
        <span class="prev-text nav-default">解决 mac 10.14.4 无法 sublime text 3207 安装 Package Control,以及安装第三方包报错 `Package Control There are no packages available for installation`</span>
        <span class="prev-text nav-mobile">上一篇</span>
      </a>
      <a class="next" href="/post/2019/2019-01-31-leetcode-solutions/">
        <span class="next-text nav-default">Leetcode 题解</span>
        <span class="next-text nav-mobile">下一篇</span>
        <i class="iconfont icon-right"></i>
      </a>
    </nav>
  </footer>
  
</article>
            </div>
            

        </div>
    </main>

    <footer id="footer" class="footer">
        <div class="social-links">
  <a href="852851198@qq.com" class="iconfont icon-email" title="email" rel="noopener" target="_blank" data-title="email"> </a>
  <a href="https://github.com/ITmxs" class="iconfont icon-github" title="github" rel="noopener" target="_blank" data-title="github"> </a>
  <a href="https://juejin.cn/user/3843548384077192" class="iconfont icon-juejin" title="juejin" rel="noopener" target="_blank" data-title="juejin"> </a>
  <a href="https://www.zhihu.com/people/yimi-yang-guang-96-65" class="iconfont icon-zhihu" title="zhihu" rel="noopener" target="_blank" data-title="zhihu"> </a>
  <a href="https://space.bilibili.com/480883651" class="iconfont icon-bilibili" title="bilibili" rel="noopener" target="_blank" data-title="bilibili"> </a>
  <a href="https://luckly.work/index.xml" type="application/rss+xml" rel="noopener" target="_blank" class="iconfont icon-rss" title="rss"></a>


</div>

<div class="copyright">
  <span class="power-by">
    由 <a class="hexo-link" href="https://gohugo.io">Hugo</a> 强力驱动
  </span>
  <span class="division">|</span>
  <span class="theme-info">
    主题 -
    <a class="theme-link" href="https://github.com/olOwOlo/hugo-theme-even">Even</a>
  </span>

  <div class="busuanzi-footer">
    <span id="busuanzi_container_site_pv"> 本站总访问量 <span id="busuanzi_value_site_pv"><img src="/img/spinner.svg" alt="spinner.svg"/></span> 次 </span>
    <span class="division">|</span>
    <span id="busuanzi_container_site_uv"> 本站总访客数 <span id="busuanzi_value_site_uv"><img src="/img/spinner.svg" alt="spinner.svg"/></span> 人 </span>
  </div>

  <span class="copyright-year">
    &copy;
    2020 -
    2021
    <span class="heart">
      <i class="iconfont icon-heart"></i>
    </span>
    <span class="author">早起的年轻人</span>
  </span>
</div>

    </footer>

    <div class="back-to-top" id="back-to-top">
        <i class="iconfont icon-up"></i>
    </div>
</div>

  <script src="https://cdn.jsdelivr.net/npm/jquery@3.2.1/dist/jquery.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/slideout@1.0.1/dist/slideout.min.js" integrity="sha256-t+zJ/g8/KXIJMjSVQdnibt4dlaDxc9zXr/9oNPeWqdg=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.js" integrity="sha256-XVLffZaxoWfGUEbdzuLi7pwaUJv1cecsQJQqGLe7axY=" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/timeago.js@3.0.2/dist/timeago.min.js" integrity="sha256-jwCP0NAdCBloaIWTWHmW4i3snUNMHUNO+jr9rYd2iOI=" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/timeago.js@3.0.2/dist/timeago.locales.min.js" integrity="sha256-ZwofwC1Lf/faQCzN7nZtfijVV6hSwxjQMwXL4gn9qU8=" crossorigin="anonymous"></script>
  <script><!-- NOTE: timeago.js uses the language code format like "zh_CN" (underscore and case sensitive) -->
    var languageCode = "zh-CN".replace(/-/g, '_').replace(/_(.*)/, function ($0, $1) {return $0.replace($1, $1.toUpperCase());});
    timeago().render(document.querySelectorAll('.timeago'), languageCode);
    timeago.cancel();  
  </script>
<script type="text/javascript" src="/dist/even.47f727f4.min.js"></script>


<script type="application/javascript">
var doNotTrack = false;
if (!doNotTrack) {
	window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
	ga('create', 'UA-138883536-1', 'auto');
	ga('set', 'anonymizeIp', true);
	ga('send', 'pageview');
}
</script>
<script async src='https://www.google-analytics.com/analytics.js'></script>







</body>
</html>
